package day5;

import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.*;
import java.util.Arrays;
import java.util.Date;

public class Homework {
    private String name;
    public int value;
    protected Date time;

    private Homework(String s){}

    public void test(){}
    public int test2(){return 0;}
    public int test3(String a,int b){return 0;}


    public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, IOException {
        demo4();
    }
    public static void demo1() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
       Class c=Class.forName("day5.Homework");
       Constructor con=c.getDeclaredConstructor(String.class);
       con.setAccessible(true);
       Homework h=(Homework) con.newInstance("aaaa");
       //invoke()   set() get()
    }
    //b.使用反射创建长度为5的String数组,为数组的
    //前三个元素赋值："A", "B", "C"。
    //打印数组的内容。(提示:Array.newInstance 来实例化数组)
    public static void demo2(){
        Object strs=Array.newInstance(String.class,5);
        Array.set(strs,0,"A");
        Array.set(strs,1,"B");
        Array.set(strs,2,"C");
        for(int i=0;i<Array.getLength(strs);i++){
            System.out.println(Array.get(strs,i));
        }
        String[] s=new String[5];
        s[0]="A";
        s[1]="B";
        s[2]="C";
        for(int i=0;i<s.length;i++){
            System.out.println(s[i]);
        }
    }
    //a.通过反射定义一个方法可以实现修改任意对象的属性值
    //    通过反射定义一个方法可以实现获取任意对象的属性值
    //    提示：设置属性  setProperty(对象，属性名,新的属性值)
    //         获取属性  getProperty(对象，属性名)
    public static void demo3(){
        Homework h=new Homework("1111");
        set(h,"name","佳豪");
        set(h,"value",1000);
        set(h,"time",new Date());
        System.out.println(get(h,"name"));
        System.out.println(get(h,"value"));
        System.out.println(get(h,"time"));

    }
    public static void set(Object obj,String name,Object value){//设置属性
        try {
            Class c=obj.getClass();
            Field f=c.getDeclaredField(name);
            f.setAccessible(true);
            f.set(obj,value);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    public static Object get(Object obj,String name){//获取属性
        Object value=null;
        try {
            Class c=obj.getClass();
            Field f=c.getDeclaredField(name);
            f.setAccessible(true);
            value=f.get(obj);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return value;
    }
    //b.通过一个类自定义的类  属性 任意  方法任意编写
    //  通过反射获取类的所有属性和方法  然后 将获取的数据
    //  写入到result.txt中  内容如下:
    //  方法有:
    //  属性有:
    //  构造方法有:
    public static void demo4() throws IOException {
        Class c=Homework.class;
        Field[] fs=c.getDeclaredFields();
        Method[] ms=c.getDeclaredMethods();
        Constructor[] cs=c.getDeclaredConstructors();
        StringBuilder sb=new StringBuilder();
        sb.append("方法有:");
        for(Method m:ms){
            String name=m.getName();
            Class[] classes=m.getParameterTypes();
            sb.append(name+" ");
        }
        sb.append("\n属性有:");
        for(Field f:fs){
            String name=f.getName();
            Class ctype=f.getType();
            sb.append(name+" ");
        }
        sb.append("\n构造有:");
        for(Constructor con:cs){
            String name=con.getName();
            sb.append(name+" ");
        }
        System.out.println(sb);

        FileWriter fw=new FileWriter("d://io//result.txt");
        fw.write(sb.toString());
        fw.close();
    }
}
